package com.crcode.crm.controller.meetingRecord;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.crcode.crm.bean.meetingRecord.MeetingRecordBean;
import com.crcode.crm.bean.user.UserBean;
import com.crcode.crm.service.meetingRecord.MeetingRecordService;
import com.crcode.crm.service.role.RoleUserService;
import com.crcode.crm.utils.DateUtil;
import com.crcode.crm.utils.PageVo;
import com.crcode.crm.utils.ResultPageUtil;
import com.crcode.crm.utils.ResultUtil;


/**
 * 会议记录控制层
 * @author cl
 * @date 2019年3月25日
 */
@Controller
public class MeetingRecordController {
	
	@Resource(name = "meetingRecordService")
	private MeetingRecordService meetingRecordService;
	
	@Autowired
	private RoleUserService roleUserService;
	
	/**
	 * 会议记录保存
	 * @param request
	 * @param session
	 * @param record
	 * @return
	 */
	@RequestMapping(value="/meetingRecord/save",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> save(HttpServletRequest request,HttpSession session,MeetingRecordBean record){
		Map<String,Object> map=new HashMap<String, Object>();
		Object obj = session.getAttribute("user");
		UserBean user = JSONObject.parseObject(obj.toString(), UserBean.class);
		if(user==null){
			map.put("status", false);
			map.put("info", "用户未登录，请先登录！");
			return map;
		}
		if(record.getId()==null){//新增
			MeetingRecordBean r=meetingRecordService.findByMeetTitleAndCreateUserUuid(record.getMeetTitle(),record.getCreateUserUuid());
			if(r!=null){
				map.put("status", false);
				map.put("info", "会议标题已存在！");
				return map;
			}
			Date now=new Date();
			record.setCreateUserUuid(user.getUuid());
			record.setCreateUserName(user.getUserRealName());
			record.setCreateTime(DateUtil.DateToStr(now, "yyyy-MM-dd HH:mm:ss"));
		}
		Boolean flag=meetingRecordService.saveResultBoolean(record);
		map.put("status", flag);
		map.put("info", "保存成功！");
		return map;
	}
	
	/**
	 * 根据id查询会议记录
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/meetingRecord/findById",method=RequestMethod.POST)
	@ResponseBody
	public MeetingRecordBean findById(HttpServletRequest request,Integer id){
		return meetingRecordService.getById(id);
	}
	
	/**
	 * 分页条件查询
	 * @param request
	 * @param conditions
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/meetingRecord/findBySpec",method=RequestMethod.POST)
	@ResponseBody
	public ResultPageUtil findBySpec(HttpServletRequest request,HttpSession session,String conditions,PageVo vo){
		Object obj = session.getAttribute("user");
		UserBean user = JSONObject.parseObject(obj.toString(), UserBean.class);
		Map<String,Object> params=JSONObject.parseObject(conditions);
		params.put("user_uuid", user.getUuid());
		params.put("isPower", this.roleUserService.findByRoleUuidAndUserUuid("admin", params.get("user_uuid").toString()).size()<=0);
		return meetingRecordService.findAll(params,vo);
	}
	
	/**
	 * 根据主键批量物理删除
	 * @param request
	 * @param session
	 * @param ids
	 * @return
	 */
	@RequestMapping(value="/meetingRecord/physicalDeleteByIds",method=RequestMethod.POST)
	@ResponseBody
	public ResultUtil physicalDeleteByIds(HttpServletRequest request,HttpSession session,String ids){
		return meetingRecordService.physicalDeleteByIds(ids);
	}
	
	/**
	 * 根据主键批量审核
	 * @param request
	 * @param session
	 * @param ids
	 * @return
	 */
	@RequestMapping(value="/meetingRecord/batchAuditByIds",method=RequestMethod.POST)
	@ResponseBody
	public ResultUtil batchAuditByIds(HttpServletRequest request,HttpSession session,String ids,Integer isStatus){
		return meetingRecordService.batchAuditByIds(ids,isStatus);
	}
	
}
